import pygame;
import re;

class GameWindow(object):
    '''
    Master pygame window for the game.
    '''
    
    textBoxheight = 100

    def __init__(self,screenWidth, screenHeight, caption):
        self._screenWidth=int(screenWidth)
        self._screenHeight=int(screenHeight)
        pygame.init()
        self._window = pygame.display.set_mode((self._screenWidth,self._screenHeight))
        pygame.display.set_caption(caption)
        
        
    def window(self):
        return self._window;
    
    def width(self):
        return self._screenWidth;
    
    def height(self):
        return self._screenHeight;
    
    @property
    def textHeight(self):
        return GameWindow.textBoxheight
    
    def _printText(self, textSurface, textToDisplay):
        textSurface.fill((0,0,0))
        textSurface.set_alpha(225);
        textSurface.convert()
        font = pygame.font.Font(None, 24)
        height = 0
        textNewlines = textToDisplay.replace("\n"," ")#remove text newlines
        textNewlines = re.sub(r"\s+"," ",textNewlines)#fix multi-spaces that may originate above
        textNewlines = textNewlines.replace("\\n","\n")#add manual newlines
        lines = []#textNewlines.splitlines()
        init =0;
        for i in range(0,len(textNewlines)):
            testString = textNewlines[init:i+1].strip()
            testString = testString.replace("\\t","    ")#tabs
            if textNewlines[i] == "\n":
                lines.append(testString)
                init = i+1
            elif textSurface.get_width()*.8<=font.size(testString)[0]:
                #back up to previous space
                j = testString.rindex(" ")
                lines.append(textNewlines[init:init+j+1].strip().replace("\\t","    "))
                init = init+j+1
        if init < len(textNewlines):
            lines.append(textNewlines[init:].strip().replace("\\t","    "))#append remaining, if exists
        width = 0
        for l in lines:
            width = max(width,font.size(l)[0])
        text = pygame.Surface((width,font.get_linesize()*len(lines)))
        for l in lines:
            t = font.render(l, 1, (200,200,200))
            text.blit(t, (0, height))
            height += font.get_linesize()
        textpos = text.get_rect()
        textpos.centerx = textSurface.get_rect().centerx
        textpos.centery = textSurface.get_rect().centery
        textSurface.blit(text, textpos)
        xpos = (self.width()-textSurface.get_width())/2
        ypos = self.height()-textSurface.get_height()
        self._window.blit(textSurface,(xpos,ypos))
        pygame.display.update();
    
    def displayLongText(self, textToDisplay):
        textSurface = pygame.Surface((self.width(), self.height()))
        self._printText(textSurface,textToDisplay)
    
    def displayText(self, textToDisplay):
        textSurface = pygame.Surface((self.width(), GameWindow.textBoxheight))
        self._printText(textSurface,textToDisplay)    
        